package com.example.administrator.newjiajiesong.network.http;



import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * retrofit2网络请求框架
 */
public class ApiUtils {
    private static MyApi.Api api;

    private static VIPApi vipApi;



    static {
        //日志拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)//链接15秒超时
                .readTimeout(15, TimeUnit.SECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                //在这里添加请求头或一些公共参数
                                .build();
                        return chain.proceed(request);
                    }
                })
                .build();

        api = new Retrofit.Builder()
                .baseUrl(MyApi.PREFIX)
                .client(mOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())//添加Gson解析
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加RxJava2支持
                .build()
                .create(MyApi.Api.class);



        vipApi = new Retrofit.Builder()
                .baseUrl(MyApi.PREFIX)
                .client(mOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(LifeTreeConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(VIPApi.class);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyApi.PREFIX)
                .client(mOkHttpClient)
                .addConverterFactory(LifeTreeConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

    }


    /**
     * 获取API
     *
     * @return
     */
    public static MyApi.Api getApi() {
        return api;
    }


    /**
     * 会员中心接口
     */
    public static VIPApi getVipApi() {
        return vipApi;
    }


}
